package com.action.school;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.action.base.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.pageModel.ClassInfo;
import com.pageModel.Json;
import com.service.school.ClassServiceI;

/**
 * 班级ACTION
 * 
 * @author chenq
 * 
 */

@Namespace("/school")
@Action(value = "classAction")
public class ClassAction extends BaseAction implements ModelDriven<ClassInfo> {

	ClassInfo classInfo = new ClassInfo();

	@Override
	public ClassInfo getModel() {
		return classInfo;
	}
	private ClassServiceI ClassService;

	public ClassServiceI getClassService() {
		return ClassService;
	}
	@Autowired
	public void setClassService(ClassServiceI classService) {
		ClassService = classService;
	}

	public void datagrid(){
		super.writeJson(ClassService.datagrid(classInfo));
	}
	
	public void getClassCmb(){
		super.writeJson(ClassService.getClassCmb(classInfo));
	}
	
	public void remove(){
		ClassService.remove(classInfo.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}
	
	public void add() {
		String json = null;
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			boolean state=ClassService.add(classInfo);
			if (state) {
				m.put("success", true);
				m.put("msg", "添加成功!");
			} else {
				m.put("success", false);
				m.put("msg", "该班级已存在!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			m.put("success", false);
			m.put("msg", "添加失败！");
			System.out.print(e.getMessage());
		}
		super.writeJson(m);
	}

	public void edit() {
		ClassService.edit(classInfo);
		Json j=new Json();
		j.setSuccess(true);
		j.setMsg("修改成功！");
		super.writeJson(j);
	}
}
